{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-02T04:53:15.184532Z",
     "iopub.status.busy": "2024-02-02T04:53:15.184532Z",
     "iopub.status.idle": "2024-02-02T04:53:15.628972Z",
     "shell.execute_reply": "2024-02-02T04:53:15.628972Z",
     "shell.execute_reply.started": "2024-02-02T04:53:15.184532Z"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "clear all\n",
    "capture log close"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-02T04:53:15.628972Z",
     "iopub.status.busy": "2024-02-02T04:53:15.628972Z",
     "iopub.status.idle": "2024-02-02T04:53:16.033109Z",
     "shell.execute_reply": "2024-02-02T04:53:16.033109Z",
     "shell.execute_reply.started": "2024-02-02T04:53:15.628972Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "version 18.0\n"
     ]
    }
   ],
   "source": [
    "version"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Import data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-02T04:53:16.033109Z",
     "iopub.status.busy": "2024-02-02T04:53:16.033109Z",
     "iopub.status.idle": "2024-02-02T04:53:16.448218Z",
     "shell.execute_reply": "2024-02-02T04:53:16.448218Z",
     "shell.execute_reply.started": "2024-02-02T04:53:16.033109Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(9 vars, 5,553 obs)\n"
     ]
    }
   ],
   "source": [
    "insheet using \"choice2.csv\", comma clear"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-02T04:53:16.448218Z",
     "iopub.status.busy": "2024-02-02T04:53:16.448218Z",
     "iopub.status.idle": "2024-02-02T04:53:16.702889Z",
     "shell.execute_reply": "2024-02-02T04:53:16.702889Z",
     "shell.execute_reply.started": "2024-02-02T04:53:16.448218Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "quarter_year  n100          p_rescue_l~a  choice        id\n",
      "n50           n900          rubber        chosen\n"
     ]
    }
   ],
   "source": [
    "ds"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-02T04:53:16.710440Z",
     "iopub.status.busy": "2024-02-02T04:53:16.710440Z",
     "iopub.status.idle": "2024-02-02T04:53:16.992106Z",
     "shell.execute_reply": "2024-02-02T04:53:16.991111Z",
     "shell.execute_reply.started": "2024-02-02T04:53:16.710440Z"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "encode choice,       gen(choice_enc)\n",
    "encode quarter_year, gen(quarter_year_enc)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-02T04:53:16.993666Z",
     "iopub.status.busy": "2024-02-02T04:53:16.993666Z",
     "iopub.status.idle": "2024-02-02T04:53:17.297184Z",
     "shell.execute_reply": "2024-02-02T04:53:17.296008Z",
     "shell.execute_reply.started": "2024-02-02T04:53:16.993666Z"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "// Allow the beta_2 to vary by boat size\n",
    "gen p_nonrescue = 1-p_rescue_libya\n",
    "\n",
    "gen p_nonrescue_50 =  p_nonrescue*n50\n",
    "gen p_nonrescue_100 = p_nonrescue*n100\n",
    "gen p_nonrescue_900 = p_nonrescue*n900"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-02T04:53:17.297184Z",
     "iopub.status.busy": "2024-02-02T04:53:17.297184Z",
     "iopub.status.idle": "2024-02-02T04:53:17.616089Z",
     "shell.execute_reply": "2024-02-02T04:53:17.616089Z",
     "shell.execute_reply.started": "2024-02-02T04:53:17.297184Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  1,851\n"
     ]
    }
   ],
   "source": [
    "count if chosen==1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Estimate models - no weights"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-02T04:53:17.627136Z",
     "iopub.status.busy": "2024-02-02T04:53:17.616089Z",
     "iopub.status.idle": "2024-02-02T04:53:21.401598Z",
     "shell.execute_reply": "2024-02-02T04:53:21.401598Z",
     "shell.execute_reply.started": "2024-02-02T04:53:17.616089Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "Iteration 0:  Log likelihood = -1297.4083  \n",
      "Iteration 1:  Log likelihood = -1057.5875  \n",
      "Iteration 2:  Log likelihood = -1050.6799  \n",
      "Iteration 3:  Log likelihood = -1050.6496  \n",
      "Iteration 4:  Log likelihood = -1050.6496  \n",
      "\n",
      "Conditional (fixed-effects) logistic regression        Number of obs =   5,553\n",
      "                                                       LR chi2(4)    = 1965.76\n",
      "                                                       Prob > chi2   =  0.0000\n",
      "Log likelihood = -1050.6496                            Pseudo R2     =  0.4833\n",
      "\n",
      "-------------------------------------------------------------------------------\n",
      "       chosen | Coefficient  Std. err.      z    P>|z|     [95% conf. interval]\n",
      "--------------+----------------------------------------------------------------\n",
      "   choice_enc |\n",
      "  100<N<=900  |   3.536819   .1579871    22.39   0.000     3.227169    3.846468\n",
      "   50<N<=100  |   1.225463     .17661     6.94   0.000     .8793142    1.571612\n",
      "              |\n",
      "p_nonresc~100 |  -1.740429   .6090019    -2.86   0.004    -2.934051   -.5468077\n",
      "p_nonresc~900 |   -5.16144   .5720086    -9.02   0.000    -6.282556   -4.040324\n",
      "-------------------------------------------------------------------------------\n"
     ]
    }
   ],
   "source": [
    "clogit chosen i.choice_enc p_nonrescue_100 p_nonrescue_900, group(id)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Estimate models - p_rescue weights"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-02T04:53:21.401598Z",
     "iopub.status.busy": "2024-02-02T04:53:21.401598Z",
     "iopub.status.idle": "2024-02-02T04:53:21.821274Z",
     "shell.execute_reply": "2024-02-02T04:53:21.821274Z",
     "shell.execute_reply.started": "2024-02-02T04:53:21.401598Z"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "gen pwr = 1/p_rescue_libya\n",
    "egen sum_pwr = sum(pwr), by(quarter_year)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-02T04:53:21.821274Z",
     "iopub.status.busy": "2024-02-02T04:53:21.821274Z",
     "iopub.status.idle": "2024-02-02T04:53:23.104733Z",
     "shell.execute_reply": "2024-02-02T04:53:23.104733Z",
     "shell.execute_reply.started": "2024-02-02T04:53:21.821274Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "Iteration 0:  Log pseudolikelihood = -1622.1785  \n",
      "Iteration 1:  Log pseudolikelihood = -1327.6782  \n",
      "Iteration 2:  Log pseudolikelihood = -1322.6394  \n",
      "Iteration 3:  Log pseudolikelihood = -1322.6045  \n",
      "Iteration 4:  Log pseudolikelihood = -1322.6045  \n",
      "\n",
      "Conditional (fixed-effects) logistic regression        Number of obs =   5,553\n",
      "                                                       Wald chi2(4)  = 1133.98\n",
      "                                                       Prob > chi2   =  0.0000\n",
      "Log pseudolikelihood = -1322.6045                      Pseudo R2     =  0.4576\n",
      "\n",
      "                                      (Std. err. adjusted for clustering on id)\n",
      "-------------------------------------------------------------------------------\n",
      "              |               Robust\n",
      "       chosen | Coefficient  std. err.      z    P>|z|     [95% conf. interval]\n",
      "--------------+----------------------------------------------------------------\n",
      "   choice_enc |\n",
      "  100<N<=900  |   3.704135    .176261    21.02   0.000      3.35867      4.0496\n",
      "   50<N<=100  |   1.339717   .1977095     6.78   0.000     .9522133     1.72722\n",
      "              |\n",
      "p_nonresc~100 |  -2.177684   .6762579    -3.22   0.001    -3.503125    -.852243\n",
      "p_nonresc~900 |  -5.985759   .5874995   -10.19   0.000    -7.137236   -4.834281\n",
      "-------------------------------------------------------------------------------\n"
     ]
    }
   ],
   "source": [
    "clogit chosen i.choice_enc p_nonrescue_100 p_nonrescue_900[pweight=pwr], group(id)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Estimate models - frequency weights"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-02T04:53:23.104733Z",
     "iopub.status.busy": "2024-02-02T04:53:23.104733Z",
     "iopub.status.idle": "2024-02-02T04:53:23.614487Z",
     "shell.execute_reply": "2024-02-02T04:53:23.614487Z",
     "shell.execute_reply.started": "2024-02-02T04:53:23.104733Z"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "egen n_incidents_per_quarter = count(id), by(quarter_year)\n",
    "\n",
    "gen pw = (1/n_incidents_per_quarter)/16\n",
    "egen sum_pw = sum(pw), by(quarter_year)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-02T04:53:23.614487Z",
     "iopub.status.busy": "2024-02-02T04:53:23.614487Z",
     "iopub.status.idle": "2024-02-02T04:53:24.514433Z",
     "shell.execute_reply": "2024-02-02T04:53:24.514433Z",
     "shell.execute_reply.started": "2024-02-02T04:53:23.614487Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "Iteration 0:  Log pseudolikelihood = -.29715617  \n",
      "Iteration 1:  Log pseudolikelihood = -.28861125  \n",
      "Iteration 2:  Log pseudolikelihood = -.26806642  \n",
      "Iteration 3:  Log pseudolikelihood = -.26793182  \n",
      "Iteration 4:  Log pseudolikelihood = -.26793169  \n",
      "Iteration 5:  Log pseudolikelihood = -.26793169  \n",
      "\n",
      "Conditional (fixed-effects) logistic regression         Number of obs =  5,553\n",
      "                                                        Wald chi2(4)  = 452.78\n",
      "                                                        Prob > chi2   = 0.0000\n",
      "Log pseudolikelihood = -.26793169                       Pseudo R2     = 0.2684\n",
      "\n",
      "                                      (Std. err. adjusted for clustering on id)\n",
      "-------------------------------------------------------------------------------\n",
      "              |               Robust\n",
      "       chosen | Coefficient  std. err.      z    P>|z|     [95% conf. interval]\n",
      "--------------+----------------------------------------------------------------\n",
      "   choice_enc |\n",
      "  100<N<=900  |   3.848612   .6043155     6.37   0.000     2.664175    5.033048\n",
      "   50<N<=100  |   1.786256   .4131352     4.32   0.000     .9765263    2.595986\n",
      "              |\n",
      "p_nonresc~100 |  -3.586908   .9548402    -3.76   0.000    -5.458361   -1.715456\n",
      "p_nonresc~900 |  -6.510521   1.997702    -3.26   0.001    -10.42595   -2.595096\n",
      "-------------------------------------------------------------------------------\n"
     ]
    }
   ],
   "source": [
    "clogit chosen i.choice_enc p_nonrescue_100 p_nonrescue_900 [pweight=pw], group(id)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Stata",
   "language": "stata",
   "name": "stata"
  },
  "language_info": {
   "codemirror_mode": "stata",
   "file_extension": ".do",
   "mimetype": "text/x-stata",
   "name": "stata",
   "version": "15.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
